package com.orphan.marketFront.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.orphan.marketFront.po.Order;

import com.orphan.marketFront.po.ReturnsPo;
import com.orphan.marketFront.vo.OrderDetailVo;
import com.orphan.marketFront.vo.OrderReturnsVo;
import com.orphan.marketFront.vo.OrderVo;
import com.orphan.util.dto.ReturnsDetailsDto;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository("orderMapper")
public interface OrderMapper extends BaseMapper<Order> {
    // ---- 订单显示 ----
    @Select("SELECT t_order.order_id,goods_type,goods_name,goods_count,order_time,goods_price, " +
            " pay_method,order_status,goods_picture,goods_count*goods_price AS amount_price " +
            " FROM t_order INNER JOIN order_detail ON t_order.order_id=order_detail.order_id " +
            " AND order_status in (0,1) and t_order.user_id=#{userId}")
    List<OrderVo> listAll(long userId);
    // ---- 通过下单时间查询 ----
    @Select("SELECT t_order.order_id,goods_type,goods_name,goods_count,order_time,goods_price,pay_method, " +
            " order_status,goods_picture,goods_count*goods_price AS amount_price " +
            " FROM t_order INNER JOIN order_detail ON t_order.order_id=order_detail.order_id and t_order.order_time like'%${orderTime}%'")
    List<OrderVo> queryByTime(String orderTime);
    // ---- 通过订单号进行删除 ----
    @Update("UPDATE t_order SET order_status=2 WHERE order_id = '${orderId}'")
    int remove(String orderId);
    // ---- 订单详情显示 ----
    @Select("SELECT t_order.order_id,goods_name,goods_count,order_status,goods_price,goods_picture,consignee,order_address,consignee_tel,goods_order_status,goods_count*goods_price AS amount_price " +
            " FROM t_order INNER JOIN order_detail ON t_order.order_id=order_detail.order_id AND t_order.order_id='${orderNo}' " )
    List<OrderDetailVo> orderDetailList(String orderNo);
    // ---- 退货订单验证 ----
    @Select("SELECT t_order.order_id,order_time,goods_name,goods_count,goods_price,goods_type " +
            " FROM t_order INNER JOIN order_detail ON t_order.order_id=order_detail.order_id " +
            " AND t_order.order_id=#{orderId} AND order_detail.goods_name=#{goodsName}")
    OrderReturnsVo orderReturns(@Param("orderId")String orderId, @Param("goodsName")String goodsName);
    // ---- 添加退货表
    @Insert("INSERT INTO order_returns(order_id,returns_no,returns_time) " +
            " VALUES(#{orderId},#{returnsNo},#{returnsTime})")
    int addOrderReturns(@Param("orderId")String orderId, @Param("returnsNo")String returnsNo,@Param("returnsTime") Date returnsTime);
    // ---- 查询退货表id
    @Select("SELECT order_returns_id FROM order_returns WHERE order_id = #{orderId}")
    List<ReturnsPo> getReturnsId(String orderId);
    // ---- 添加退货详情表
    @Insert("INSERT INTO order_detail2(order_returns_id,goods_type,goods_name,goods_price,goods_count,goods_order_status) " +
            " VALUES(#{orderReturnsId},#{goodsType},#{goodsName},#{goodsPrice},#{goodsCount},#{goodsOrderStatus})")
    int addReturnsDetails(@Param("orderReturnsId")Long orderReturnsId, @Param("goodsType")String goodsType,@Param("goodsName") String goodsName,
                          @Param("goodsPrice")Double goodsPrice, @Param("goodsCount")int goodsCount,@Param("goodsOrderStatus") int goodsOrderStatus);
    // ---- 修改订单商品详情表的商品订单状态
    @Update("UPDATE order_detail SET goods_order_status=#{goodsOrderStatus} WHERE goods_name=#{goodsName}")
    int updateStatus(@Param("goodsOrderStatus") int goodsOrderStatus,@Param("goodsName") String goodsName);
    // ---- 获取退货订单信息发送给商城后端
    @Select("SELECT t_order.order_id,returns_no,returns_time,goods_name,goods_type,goods_price,goods_count,goods_order_status FROM t_order " +
            " INNER JOIN order_returns on t_order.order_id=order_returns.order_id and t_order.order_id=#{orderId} " +
            " INNER JOIN order_detail2 on order_returns.order_returns_id=order_detail2.order_returns_id ")
    List<ReturnsDetailsDto> returnsDetails(String orderId);

    @Update("update t_order set pay_status = #{payStatus} where order_id = #{orderId}")
    Integer updateOrderPayStatus(@Param("payStatus") String orderId,@Param("orderId") Integer payStatus);
}
